<?php

namespace app\api\model;

use Exception;
use frame\app\Model;
use frame\lib\Db;

class Common extends Model
{
    /**
     * @param int $type 1 增加每日的App启动 2 增加每日登录次数
     */
    public static function statistics($user_id, $type = 1)
    {
        try {
            $heat = Db::name('statistics')->where(['user_id' => $user_id])->find();
            if ($heat['today'] != date('d', time())) {
                Db::name('statistics')->where(['user_id' => $user_id])->update(['today' => date('d')]);
            }
            if ($type == 1) {
                $data = ['heat' => ++$heat['heat']];
            } else if ($type == 2) {
                $data = ['log' => ++$heat['log']];
            }
            Db::name('statistics')->where(['user_id' => $user_id])->update($data);
        } catch (Exception $e) {
            
        }
    }
    /**
     * 检测用户接口配置是否正确
     */
    public static function verifyConf($param)
    {
        if (empty($param['user_id'])) error('接口配置错误：缺少参数user_id', 202);
        // 一级用户ID
        $user_id = Db::name('user')->where(['id' => $param['user_id']])->find();
        if (!$user_id) error('接口配置错误：user_id不存在', 202);
        // 一级用户非会员状态，停用接口调用
        if (getTime(3, $user_id['vip_time'])  < time()) error('用户已到期，无法发送请求，续费后可正常使用');
        $conf = Db::name('config')->where(['user_id' => $param['user_id']])->find();
        return json_decode($conf['conf'], true);
    }
}
